package com.fla.aiba.service.impl;

import com.fla.aiba.dto.LiveDto;
import com.fla.aiba.service.LiveService;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * Created by qudingyi on 2017/5/2.
 */
@Service
public class LiveServiceImpl implements LiveService {
    private static  final String BASE_LIBE_SQL="select \n" +
            "u.age,\n" +
            "u.avatar,\n" +
            "u.geohash,\n" +
            "u.id as userId,\n" +
            "u.nick_name as nickName,\n" +
            "u.login_city as city,\n" +
            "le.live_id as liveId,\n" +
            "le.notice,\n" +
            "le.title,\n" +
            "from t_user u\n" +
            "LEFT JOIN t_live le on le.user_id=u.id\n" +
            "GROUP BY u.id ";

    @PersistenceUnit
    private EntityManagerFactory emf;
    @Override
    public List<Map<String, Object>> findFollowMy(String userId) {
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery("select u.age,u.avatar,u.birthday,u.nick_name as nickName,u.sex,u.signature from t_live_follow lf\n" +
                "INNER JOIN t_user u on u.id=lf.follow_use_id\n" +
                "where lf.follow_use_id=:userid");
        query.setParameter("userid",userId);
        query.unwrap(org.hibernate.SQLQuery.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String,Object>> ob = (List<Map<String,Object>>) query.getResultList();
        em.close();
        return  ob;


    }

    @Override
    public List<Map<String, Object>> findMyFollow(String userId) {
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery("select u.age,u.avatar,u.birthday,u.nick_name as nickName,u.sex,u.signature from t_live_follow lf\n" +
                "INNER JOIN t_user u on u.id=lf.user_id\n" +
                "where lf.user_id=:userId");
        query.setParameter("userId",userId);
        query.unwrap(org.hibernate.SQLQuery.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String,Object>> ob = (List<Map<String,Object>>) query.getResultList();
        em.close();
        return  ob;

    }

    @Override
    public LiveDto findByUserId(String userId) {
        EntityManager em=emf.createEntityManager();
        StringBuffer buffer=new StringBuffer("select v.* from (");
        buffer.append(BASE_LIBE_SQL);
        buffer.append(") as v ");
        buffer.append("where v.userId=:userId ");
        Query nativeQuery = em.createNativeQuery(buffer.toString());
        nativeQuery.setParameter("userId",userId);
        SQLQuery sqlQuery= nativeQuery.unwrap(SQLQuery.class);
        sqlQuery.addScalar("myfollowNumber", StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("foolowNumber", StandardBasicTypes.INTEGER);
        sqlQuery.addScalar("liveId", StandardBasicTypes.LONG);
        sqlQuery.addScalar("exp", StandardBasicTypes.INTEGER);
        sqlQuery.setResultTransformer(Transformers.aliasToBean( LiveDto.class));
        LiveDto view= (LiveDto) nativeQuery.getSingleResult();
        em.close();
        return view;
    }


}
